const app = getApp();
var WxParse = require('../../wxParse/html2json.js');
Page({
  data: {
    name:'',
    phone:'',
    address:'',
    yunfei:0,
    msg1:'',
    msg2:'',
    winWidth: 0,
    winHeight: 0,
    currentTab: 0,
    
    proList:[]
  },
  onShow: function () {
    var pages = getCurrentPages();
    var currPage = pages[pages.length - 1];
    console.log("列表页传过来的值", currPage.data);//此处既是上一页面传递过来的值
    this.setData({
      deliveryId: currPage.data.dlid,
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log('options:',options)
    var that = this;
    // 获取系统信息
    wx.getSystemInfo({
      success: function (res) {
        that.setData({
          winWidth: res.windowWidth,
          winHeight: res.windowHeight
        });
      }
    });
    //普通商品0 购物车1 
    if(options.isPro==0){
      let numList = options.number.split(',')
      console.log("numList", numList)
      let totalNum = 0
      for (var i = numList.length - 1; i >= 0; i--) {
        totalNum += Number(numList[i]);
      }

      that.setData({
        ids: options.ids,
        number: options.number,
        money: options.money,
        totalNum: totalNum,
        total_price: options.money
      })

      that.shoppingRequest(options)

    } else if(options.isPro == 1){
      that.setData({
        ids: options.ids,
        number: options.number,
        money: options.money,
        totalNum: options.number,
        total_price: options.money
      })
      that.proRequest(options)
    } 
  },

  //购物车进到购物清单
  shoppingRequest:function(options){
    var that = this
    wx.getStorage({
      key: 'openid',
      success: function (res) {
        let openid = res.data

        wx.request({
          url: app.globalData.URL + '/app/product/productActivity?openid=' + openid + '&ids=' + options.ids,
          success: function (res) {
            console.log("productActivity", res)
            if (res.data.flag == true) {

              wx.request({
                url: app.globalData.URL + '/app/product/userRelatio?openid=' + openid,
                success: function (res) {
                  console.log("userRelatio", res)
                  if (res.data.flag == true) {

                    wx.request({
                      url: app.globalData.URL + '/app/product/purchase?openid=' + openid + '&ids=' + options.ids + '&number=' + options.number,
                      success: function (res) {
                        console.log("purchase", res)
                        if(res.data.flag == true){
                          that.setData({
                            paseImg: that.parseImg(res.data.qhcPro, that),
                            since: res.data.since[0].id, //自提id
                            storefrontAddress: res.data.since[0].storefrontAddress,
                            storefrontName: res.data.since[0].storefrontName,
                            storefrontTel: res.data.since[0].storefrontTel,
                            name: res.data.delivery.name,
                            phone: res.data.delivery.telPhone,
                            address: res.data.delivery.address,
                            deliveryId: res.data.delivery.id,
                            proList: res.data.qhcPro
                          })
                        }else{
                          wx.showModal({
                            title: '',
                            content: '您购买的商品没有预约',
                            success: function (res) {
                              if (res.cancel) {
                                wx.navigateBack({})
                              } else {
                                wx.navigateBack({})
                              }
                            },
                          })
                        }                        
                      }
                    })
                  } else {
                    wx.showModal({
                      title: '',
                      content: res.data.msg,
                      success: function (res) {
                        if (res.cancel) {
                          wx.navigateBack({})
                        } else {
                          wx.navigateBack({})
                        }
                      },
                    })
                  }
                }
              })
            } else {
              wx.showModal({
                title: '',
                content: res.data.msg,
                success: function (res) {
                  if (res.cancel) {
                    wx.navigateBack({})
                  } else {
                    wx.navigateBack({})
                  }
                },
              })
            }
          }
        })
      },
    })
  },
  //商品直接进入购物清单
  proRequest: function (options){
    var that = this
    wx.getStorage({
      key: 'openid',
      success: function(res) {
        let openid = res.data

        wx.request({
          url: app.globalData.URL + '/app/product/userRelatio?openid=' + openid,
          success:function(res){
            if(res.data.flag == true){
              wx.request({
                url: app.globalData.URL + '/app/product/settlement?openid=' + openid + '&proId=' + options.ids + '&number=' + options.number + '&money=' + options.money,
                success:function(res){
                  console.log("product/settlement",res.data)
                  that.setData({
                    paseImg: that.parseImg(res.data.qhcPro, that),
                    since: res.data.since[0].id, //自提id
                    storefrontAddress: res.data.since[0].storefrontAddress,
                    storefrontName: res.data.since[0].storefrontName,
                    storefrontTel: res.data.since[0].storefrontTel,
                    name: res.data.delivery.name,
                    phone: res.data.delivery.telPhone,
                    address: res.data.delivery.address,
                    deliveryId: res.data.delivery.id,
                    proList: res.data.qhcPro
                  })
                }
              })
            }else{
              wx.showModal({
                title: '',
                content: res.data.msg,
                success: function (res) {
                  if (res.cancel) {
                    wx.navigateBack({})
                  } else {
                    wx.navigateBack({})
                  }
                },
              })
            }
          }
        })
      },
    })
  },
  
  // 滑动切换tab
  bindChange: function (e) {
    var that = this;
    that.setData({ currentTab: e.detail.current });
  },
  swichNav: function (e) {
    var that = this;
    if (this.data.currentTab === e.target.dataset.current) {
      return false;
    } else {
      that.setData({
        currentTab: e.target.dataset.current
      })
    }
  },
  toAddressList:function(){
    wx.navigateTo({
      url: '../address/address?type=1',
    })
  },
  
  parseImg: function (data, that) {
    var parseImgObj = {}
    for (var i = 0; i < data.length; i++) {
      parseImgObj[data[i].shoppingId] = WxParse.html2json(data[i]['productClassPic'], 'returnData');
    }
    return parseImgObj
  },

  leaveMsg1: function (e) {
    this.setData({
      msg1: e.detail.value
    })
  },

  leaveMsg2:function(e){
    this.setData({
      msg2: e.detail.value
    })
  },

  //商家配送去支付
  shangjia:function(){
    if(this.data.name == ''){
      wx.showToast({
        title:"请选择收货地址",
        image: '../../images/error.png',
      })
    }else{
      // var fei = 0;
      // var arr = new Array("福建", "内蒙", "广西", "海南", "甘肃", "宁夏", "青海", "云南", "贵州", "西藏", "新疆");
      // for (var i = 0; i < arr.length; i++) {
      //   if (arr[i] == str.substring(0, 2)) {
      //     if ("西藏" == str.substring(0, 2)) {
      //       fei = (total - num) * 500;
      //     } else if ("新疆" == str.substring(0, 2)) {
      //       fei = (total - num) * 500;
      //     } else if (memberType == "0") {
      //       fei = (total - num) * 5;
      //     }
      //   }
      // }

      wx.navigateTo({
        url: '../pay/pay?ids=' + this.data.ids + '&money=' + this.data.money + '&number=' + this.data.number + '&deliveryId=' + this.data.deliveryId + '&type=0' + '&msg=' + this.data.msg1,
      })
    }
    
  },
  //自提去支付
  ziti:function(){
    if (this.data.name == '') {
      wx.showToast({
        title: "请选择收货地址",
        image: '../../images/error.png',
      })
    } else {
      
      wx.navigateTo({
        url: '../pay/pay?ids=' + this.data.ids + '&money=' + this.data.money + '&number=' + this.data.number + '&since=' + this.data.since + '&type=1' + '&msg=' + this.data.msg1,
      })
    }
  }
  
})